home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / gettabct.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  328 lines

  1.  
  2. #include <windows.h>  
  3. #include "gettabct.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. HWND    hPropSheet  = NULL;
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "PropSheet_GetTabControl"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       if ( IsWindow( hPropSheet ) && PropSheet_IsDialogMessage( hPropSheet, &msg ) )
  75.          continue;
  76.  
  77.       TranslateMessage( &msg ); 
  78.       DispatchMessage( &msg );  
  79.    }
  80.  
  81.    return( msg.wParam ); 
  82. }
  83.  
  84.  
  85. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  86. {
  87.    WNDCLASSEX wcex;
  88.  
  89.    wcex.style         = lpwc->style;
  90.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  91.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  92.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  93.    wcex.hInstance     = lpwc->hInstance;
  94.    wcex.hIcon         = lpwc->hIcon;
  95.    wcex.hCursor       = lpwc->hCursor;
  96.    wcex.hbrBackground = lpwc->hbrBackground;
  97.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  98.    wcex.lpszClassName = lpwc->lpszClassName;
  99.  
  100.    // Added elements for Windows 95.
  101.    //...............................
  102.    wcex.cbSize = sizeof(WNDCLASSEX);
  103.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  104.                             IMAGE_ICON, 16, 16,
  105.                             LR_DEFAULTCOLOR );
  106.             
  107.    return RegisterClassEx( &wcex );
  108. }
  109.  
  110.  
  111.  
  112. LRESULT CALLBACK Page1Proc( HWND hDlg, UINT message,        
  113.                             WPARAM wParam, LPARAM lParam )
  114. {
  115.    switch (message) 
  116.    {
  117.        case WM_INITDIALOG : 
  118.                return (TRUE);
  119.  
  120.        case WM_NOTIFY :
  121.                switch( ((NMHDR*)lParam)->code )
  122.                {
  123.                   case PSN_APPLY :
  124.                   case PSN_QUERYCANCEL :
  125.                          DestroyWindow( hPropSheet );
  126.                          break;
  127.                }
  128.                break;
  129.    }
  130.  
  131.    return( FALSE );
  132. }
  133.  
  134.  
  135. LRESULT CALLBACK Page2Proc( HWND hDlg, UINT message,        
  136.                             WPARAM wParam, LPARAM lParam )
  137. {
  138.    switch (message) 
  139.    {
  140.        case WM_INITDIALOG : 
  141.                return (TRUE);
  142.  
  143.        case WM_NOTIFY :
  144.                switch( ((NMHDR*)lParam)->code )
  145.                {
  146.                   case PSN_APPLY :
  147.                   case PSN_QUERYCANCEL :
  148.                          DestroyWindow( hPropSheet );
  149.                          break;
  150.                }
  151.                break;
  152.    }
  153.  
  154.    return( FALSE );
  155. }
  156.  
  157.  
  158. LRESULT CALLBACK Page3Proc( HWND hDlg, UINT message,        
  159.                             WPARAM wParam, LPARAM lParam )
  160. {
  161.    switch (message) 
  162.    {
  163.        case WM_INITDIALOG : 
  164.                return (TRUE);
  165.  
  166.        case WM_NOTIFY :
  167.                switch( ((NMHDR*)lParam)->code )
  168.                {
  169.                   case PSN_APPLY :
  170.                   case PSN_QUERYCANCEL :
  171.                          DestroyWindow( hPropSheet );
  172.                          break;
  173.                }
  174.                break;
  175.    }
  176.  
  177.    return( FALSE );
  178. }
  179.  
  180.  
  181. VOID DisplayProperties( HWND hWnd )
  182. {
  183.    PROPSHEETPAGE   psp;
  184.    PROPSHEETHEADER psh;
  185.    HPROPSHEETPAGE  hpsp[3];
  186.    TOOLINFO        ti;
  187.    HWND            hToolTip;
  188.    HWND            hTabCtrl;
  189.    int             nCnt, index;
  190.  
  191.    psp.dwSize    = sizeof( PROPSHEETPAGE );
  192.    psp.dwFlags   = PSP_DEFAULT;
  193.    psp.hInstance = hInst;
  194.  
  195.    psp.pszTemplate = "PAGE1";
  196.    psp.pfnDlgProc  = (DLGPROC)Page1Proc;
  197.    hpsp[0] = CreatePropertySheetPage( &psp );
  198.  
  199.    psp.pszTemplate = "PAGE2";
  200.    psp.pfnDlgProc  = (DLGPROC)Page2Proc;
  201.    hpsp[1] = CreatePropertySheetPage( &psp );
  202.  
  203.    psp.pszTemplate = "PAGE3";
  204.    psp.pfnDlgProc  = (DLGPROC)Page3Proc;
  205.    hpsp[2] = CreatePropertySheetPage( &psp );
  206.  
  207.    // Initialize the property sheet structure.
  208.    //.........................................
  209.    memset( &psh, 0, sizeof( PROPSHEETHEADER ) );
  210.    psh.dwSize     = sizeof( PROPSHEETHEADER );
  211.    psh.dwFlags    = PSH_USEICONID | PSH_MODELESS | PSH_NOAPPLYNOW;
  212.    psh.hInstance  = hInst;
  213.    psh.hwndParent = hWnd;
  214.    psh.pszIcon    = "SMALL";
  215.    psh.nPages     = 3;
  216.    psh.phpage     = hpsp;
  217.    psh.pszCaption = "Properties";
  218.  
  219.    // Create a modeless property sheet.
  220.    //..................................
  221.    hPropSheet = (HWND)PropertySheet( &psh );
  222.  
  223.    // Retrieve a handle to the tab control.
  224.    //......................................
  225.    hTabCtrl   = PropSheet_GetTabControl( hPropSheet );
  226.  
  227.    // Create tool tip control.
  228.    //.........................
  229.    hToolTip = CreateWindowEx( 0,
  230.                               TOOLTIPS_CLASS, NULL, 
  231.                               TTS_ALWAYSTIP,
  232.                               CW_USEDEFAULT, 
  233.                               CW_USEDEFAULT, 
  234.                               CW_USEDEFAULT, 
  235.                               CW_USEDEFAULT,
  236.                               hWnd, NULL, 
  237.                               hInst, NULL );
  238.  
  239.    // Add the tools to the tool tip control.
  240.    //.......................................
  241.    ti.cbSize = sizeof( TOOLINFO );
  242.    ti.hwnd   = hTabCtrl;
  243.    ti.hinst  = hInst;
  244.    ti.uFlags = TTF_SUBCLASS;
  245.  
  246.    // Retrieve the number of tabs.
  247.    //.............................
  248.    nCnt = TabCtrl_GetItemCount( hTabCtrl );
  249.  
  250.    // Assign the tools to the tab control.
  251.    //.....................................
  252.    for( index=0; index < nCnt; index++ )
  253.    {
  254.       TabCtrl_GetItemRect( hTabCtrl, index, &ti.rect );
  255.  
  256.       ti.uId      = index+1;
  257.       ti.lpszText = (LPTSTR)index+1;
  258.  
  259.       SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  260.    }
  261.  
  262.    // Associate the tooltip control with the tab control.
  263.    //....................................................
  264.    TabCtrl_SetToolTips( hTabCtrl, hToolTip );
  265. }
  266.  
  267.  
  268. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  269. {
  270.    switch( uMsg )
  271.    {
  272.       case WM_CREATE :
  273.               InitCommonControls();
  274.               break;
  275.  
  276.       case WM_COMMAND :
  277.               switch( LOWORD( wParam ) )
  278.               {
  279.                  case IDM_TEST :
  280.                         if ( !IsWindow( hPropSheet ) )
  281.                            DisplayProperties( hWnd );
  282.                         break;
  283.  
  284.                  case IDM_ABOUT :
  285.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  286.                         break;
  287.  
  288.                  case IDM_EXIT :
  289.                         DestroyWindow( hWnd );
  290.                         break;
  291.               }
  292.               break;
  293.       
  294.       case WM_DESTROY :
  295.               PostQuitMessage(0);
  296.               break;
  297.  
  298.       default :
  299.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  300.    }
  301.  
  302.    return( 0L );
  303. }
  304.  
  305.  
  306. LRESULT CALLBACK About( HWND hDlg,           
  307.                         UINT message,        
  308.                         WPARAM wParam,       
  309.                         LPARAM lParam)
  310. {
  311.    switch (message) 
  312.    {
  313.        case WM_INITDIALOG: 
  314.                return (TRUE);
  315.  
  316.        case WM_COMMAND:                              
  317.                if (   LOWORD(wParam) == IDOK         
  318.                    || LOWORD(wParam) == IDCANCEL)    
  319.                {
  320.                        EndDialog(hDlg, TRUE);        
  321.                        return (TRUE);
  322.                }
  323.                break;
  324.    }
  325.  
  326.    return (FALSE); 
  327. }
  328.